package com.woniuxy.project115.kaer.tenant.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.domain.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.project115.kaer.commons.util.JwtUtil;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.tenant.entity.Appointment;
import com.woniuxy.project115.kaer.tenant.form.AddAppointmentForm;
import com.woniuxy.project115.kaer.tenant.form.GetAppointmentForm;
import com.woniuxy.project115.kaer.tenant.param.AddAppointmentParam;
import com.woniuxy.project115.kaer.tenant.param.GetAppointmentParam;
import com.woniuxy.project115.kaer.tenant.service.AppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuH
 * @since 2025年02月18日
 */
@RestController
@RequestMapping("/appointment")
@Api(tags = "服务预约接口")
public class AppointmentController {
    @Resource
    private AppointmentService appointmentService;

    @PostMapping("/addAppointment")
    @ApiOperation("新增服务预约")
    public Result addAppointment(@RequestBody AddAppointmentForm form, @RequestHeader String token) throws Exception {
        Integer userId = JwtUtil.getUserId(token);
        appointmentService.addAppointment(BeanUtil.toBean(form, AddAppointmentParam.class), userId);
        return Result.succeed("新增成功");
    }

    @PostMapping("/userCancelAppointment")
    @ApiOperation("用户取消服务预约")
    @ApiImplicitParam(name = "appointmentId", value = "服务预约id")
    public Result userCancelAppointment(Integer appointmentId) throws Exception {
        appointmentService.userCancelAppointment(appointmentId);
        return Result.succeed("取消成功");
    }

    @PostMapping("/confirm")
    @ApiOperation("确认服务，下单")
    @ApiImplicitParam(name = "appointmentId", value = "服务预约id")
    public Result confirm(Integer appointmentId) throws Exception {
         appointmentService.confirm(appointmentId);
         return Result.succeed("下单成功");
    }

    @PostMapping("/getAppointment")
    @ApiOperation("获取服务预约")
    public Result getAppointment(@RequestBody GetAppointmentForm form, @RequestHeader String token) throws Exception {
        Integer storeId = JwtUtil.getStoreId(token);
        Page<Appointment> page = appointmentService.getAppointment(BeanUtil.toBean(form, GetAppointmentParam.class), storeId);
        return Result.succeed("success", page);
    }

}

